package com.jdrx.ocp.api;

import com.jdrx.ocp.beans.constans.ServiceGroupEnum;
import com.jdrx.ocp.beans.constans.ServiceTypeEnum;
import com.jdrx.ocp.beans.dto.platform.*;
import com.jdrx.ocp.beans.vo.ServiceGroupVO;
import com.jdrx.ocp.service.PlatformApiService;
import com.jdrx.ocp.service.PlatformService;
import com.jdrx.platform.common.support.gateway.GwConstants;
import com.jdrx.platform.commons.rest.beans.dto.IdDTO;
import com.jdrx.platform.commons.rest.beans.enums.EApiStatus;
import com.jdrx.platform.commons.rest.beans.vo.ResposeVO;
import com.jdrx.platform.commons.rest.exception.BizException;
import com.jdrx.platform.commons.rest.factory.ResponseFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.FileNotFoundException;
import java.util.ArrayList;

import static com.jdrx.platform.common.support.gateway.GwConstants.TRANSPARENT_TOKEN_FEILD;
import static com.jdrx.platform.common.support.gateway.GwConstants.TRANSPARENT_USERID_FEILD;

/**
 * @ClassName:PlatformApi
 * @Description:平台管理模块
 * @author: yangfang
 * @date: 2018/11/19 13:41
 */
@RestController
@Api(value = "平台管理模块", description = "平台管理模块")
@RequestMapping(value = "/api/0/ocp/platform")
public class PlatformApi {

    @Autowired
    private PlatformService platformService;
    @Autowired
    private PlatformApiService platformApiService;

    @ApiOperation(value="分页查询平台信息", notes="分页查询平台信息")
    @RequestMapping(value="queryPlatformByPage", method= RequestMethod.POST)
    @ResponseBody
    public ResposeVO queryPlatformByPage(@RequestBody @Valid QueryPlatformDTO dto) throws BizException{
        return ResponseFactory.ok(platformService.queryPlatformByPage(dto));
    }

    @ApiOperation(value="查询所有应用服务信息以及接口统计信息", notes="查询所有应用服务信息以及接口统计信息（替换apicenter中的 queryService）")
    @RequestMapping(value="platformApi", method= RequestMethod.POST)
    public ResposeVO platformApi(@RequestBody PlatformDTO platformDTO
            ,@RequestHeader(TRANSPARENT_TOKEN_FEILD)String token) throws BizException{
        return ResponseFactory.ok(platformService.queryListByEnable(platformDTO,token));
    }

    @ApiOperation(value="获取应用服务统计信息", notes="获取应用服务统计信息（替换apicenter中的 getStatisticNum）")
    @RequestMapping(value="getStatisticNum", method= RequestMethod.POST)
    public ResposeVO getStatisticNum(@RequestHeader(TRANSPARENT_TOKEN_FEILD)String token) throws BizException{
        return ResponseFactory.ok(platformService.queryServiceApiCount(token));
    }

    //@BizLogger(description = "创建平台信息", serviceId = Constants.SERVICE_ID)
    @ApiOperation(value = "创建平台信息", notes = "创建平台信息")
    @RequestMapping(value = "createPlatform", method = RequestMethod.POST)
    public @ResponseBody ResposeVO createPlatform(@RequestBody @Valid CreatePlatformDTO dto,
                                                  @RequestHeader(TRANSPARENT_USERID_FEILD)String uid)throws BizException{
        platformService.createPlatform(dto, Long.valueOf(uid), ServiceTypeEnum.NEW_SERVICE.getType());
        return ResponseFactory.ok(EApiStatus.SUCCESS);
    }

    //@BizLogger(description = "创建平台信息(旧系统配置)", serviceId = Constants.SERVICE_ID)
    @ApiOperation(value = "创建平台信息(旧系统配置)", notes = "创建平台信息(旧系统配置)")
    @RequestMapping(value = "createPlatformExtend", method = RequestMethod.POST)
    public @ResponseBody ResposeVO createPlatformExtend(@RequestBody @Valid CreatePlatformExtendDTO dto,
                                                  @RequestHeader(TRANSPARENT_USERID_FEILD)String uid)throws BizException{
        platformService.createPlatformExtend(dto, Long.valueOf(uid));
        return ResponseFactory.ok(EApiStatus.SUCCESS);
    }

    //@BizLogger(description = "修改平台信息(旧系统配置)", serviceId = Constants.SERVICE_ID)
    @ApiOperation(value = "修改平台信息(旧系统配置)", notes = "修改平台信息(旧系统配置)")
    @RequestMapping(value = "updatePlatformExtend", method = RequestMethod.POST)
    public @ResponseBody ResposeVO updatePlatformExtend(@RequestBody @Valid UpdatePlatformExtendDTO dto,
                                                  @RequestHeader(TRANSPARENT_USERID_FEILD)String uid)throws BizException{
        platformService.updatePlatformExtend(dto, Long.valueOf(uid));
        return ResponseFactory.ok(EApiStatus.SUCCESS);
    }

    //@BizLogger(description = "修改平台信息", serviceId = Constants.SERVICE_ID)
    @ApiOperation(value = "修改平台信息", notes = "修改平台信息")
    @RequestMapping(value = "updatePlatform", method = RequestMethod.POST)
    public @ResponseBody ResposeVO updatePlatform(@RequestBody @Valid UpdatePlatformDTO dto,
                                                  @RequestHeader(TRANSPARENT_USERID_FEILD)String uid)throws BizException{
        platformService.updatePlatform(dto, Long.valueOf(uid));
        return ResponseFactory.ok(EApiStatus.SUCCESS);
    }

    //@BizLogger(description = "删除平台信息", serviceId = Constants.SERVICE_ID)
    @ApiOperation(value = "删除平台信息", notes = "删除平台信息")
    @RequestMapping(value = "deletePlatform", method = RequestMethod.POST)
    public @ResponseBody ResposeVO deletePlatform(@RequestBody @Valid IdDTO<String> id,
                                                  @RequestHeader(TRANSPARENT_USERID_FEILD)String uid)throws BizException{
        platformService.deletePlatform(Long.valueOf(id.getId()),Long.valueOf(uid));
        return ResponseFactory.ok(EApiStatus.SUCCESS);
    }

    //@BizLogger(description = "启停信息", serviceId = Constants.SERVICE_ID)
    @ApiOperation(value = "启停信息", notes = "启停信息")
    @RequestMapping(value = "upateUseable", method = RequestMethod.POST)
    public @ResponseBody ResposeVO upateUseable(@RequestBody @Valid UpdateUseableDTO updateUseableDTO,
                                                  @RequestHeader(TRANSPARENT_USERID_FEILD)String uid)throws BizException{
        if(platformService.updateUseable(updateUseableDTO,Long.valueOf(uid))){
            return ResponseFactory.ok("启停成功！");
        }else{
            return ResponseFactory.err("启停失败！",EApiStatus.ERR_SYS);
        }
    }

    //@BizLogger(description = "创建平台-API授权信息", serviceId = Constants.SERVICE_ID)
    @ApiOperation(value = "创建平台-API授权信息", notes = "创建平台-API授权信息")
    @RequestMapping(value = "createPlatformApiAuth", method = RequestMethod.POST)
    public @ResponseBody ResposeVO createPlatformApiAuth(@RequestBody @Valid CreatePlatformApiDTO dto)throws BizException{
        platformApiService.createPlatformApiAuth(dto);
        return ResponseFactory.ok(EApiStatus.SUCCESS);
    }

    //@BizLogger(description = "创建平台-API授权信息-数组结构", serviceId = Constants.SERVICE_ID)
    @ApiOperation(value = "创建平台-API授权信息-数组结构", notes = "创建平台-API授权信息-数组结构")
    @RequestMapping(value = "createPlatformApiAuthByList", method = RequestMethod.POST)
    public @ResponseBody ResposeVO createPlatformApiAuthByList(@RequestBody @Valid CreatePlatformApiByListDTO dto)throws BizException{
        platformApiService.createPlatformApiAuth(dto);
        return ResponseFactory.ok(EApiStatus.SUCCESS);
    }

    //@BizLogger(description = "查询平台-API授权关系数据", serviceId = Constants.SERVICE_ID)
    @ApiOperation(value = "查询平台-API授权关系数据", notes = "查询平台-API授权关系数据")
    @RequestMapping(value = "queryPlatformApiAuth", method = RequestMethod.POST)
    public @ResponseBody ResposeVO queryPlatformApiAuth(@RequestBody @Valid QueryPlatformPrefixDTO dto, @RequestHeader(TRANSPARENT_TOKEN_FEILD)String token)throws BizException{
        return ResponseFactory.ok(platformApiService.queryPlatformApiAuth(token, dto));
    }

    //@BizLogger(description = "查询平台-API授权关系统计数据", serviceId = Constants.SERVICE_ID)
    @ApiOperation(value = "查询平台-API授权关系统计数据", notes = "查询平台-API授权关系统计数据")
    @RequestMapping(value = "queryPlatformAuth", method = RequestMethod.POST)
    public @ResponseBody ResposeVO queryPlatformAuth()throws BizException{
        return ResponseFactory.ok(platformApiService.queryPlatformAuth());
    }

    //@BizLogger(description = "服务以及授权二维表数据接口", serviceId = Constants.SERVICE_ID)
    @ApiOperation(value = "服务以及授权二维表数据接口", notes = "服务以及授权二维表数据接口")
    @RequestMapping(value = "servicesAuth", method = RequestMethod.POST)
    public @ResponseBody ResposeVO servicesAuth(@RequestHeader(TRANSPARENT_TOKEN_FEILD)String token)throws BizException{
        return ResponseFactory.ok(platformApiService.platformAuth(token));
    }

    //@BizLogger(description = "获取平台前缀", serviceId = Constants.SERVICE_ID)
    @ApiOperation(value = "获取平台前缀", notes = "获取平台前缀")
    @RequestMapping(value = "queryPlatformPrefix", method = RequestMethod.POST)
    public @ResponseBody ResposeVO queryPlatformPrefix(@RequestHeader(TRANSPARENT_TOKEN_FEILD)String token)throws BizException{
        QueryPlatformPrefixDTO queryPlatformPrefixDTO = new QueryPlatformPrefixDTO();
        return ResponseFactory.ok(platformService.queryPlatformPrefix(queryPlatformPrefixDTO));
    }

    @ApiOperation(value="导出平台API", notes="导出平台API")
    @RequestMapping(value="exportPlatformApi", method= RequestMethod.POST)
    public ResposeVO exportPlatformApi(@RequestBody @Valid ExportPlatformApiDTO dto,
                                        @RequestHeader(GwConstants.TRANSPARENT_TOKEN_FEILD) String token,
                                        @RequestHeader(value = GwConstants.TRANSPARENT_USERID_FEILD) String userId,
                                        HttpServletResponse response) throws FileNotFoundException, BizException {
        platformApiService.exportPlatformApi(dto, token, userId, response);
        return ResponseFactory.ok(EApiStatus.SUCCESS);
    }

    @ApiOperation(value="获取服务分组", notes="获取服务分组")
    @RequestMapping(value="findServiceGroup", method= RequestMethod.POST)
    public ResposeVO findServiceGroup() throws BizException {

        ArrayList <ServiceGroupVO> list = new ArrayList<>();
        for (ServiceGroupEnum serviceGroup : ServiceGroupEnum.values()) {
            list.add(new ServiceGroupVO(serviceGroup.getType(),serviceGroup.getDesc()));
        }

        return ResponseFactory.ok(list);
    }

}
